﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

using CP.Entities;
using CP.Controllers.GPM;
using DevExpress.XtraEditors;
using CP.Utility;

namespace GPM
{
    public partial class FrmEditUserGroup : BaseEditForm
    {
        UserGroupController controller = new UserGroupController();
        public int CurrentGroupID { get; private set; }
        public FrmEditUserGroup()
        {
            InitializeComponent();
        }

        public FrmEditUserGroup(string id, int groupid):this()
        {
            base.EntityID = id;
            this.CurrentGroupID = groupid;
        }

        /// <summary>
        /// 编辑或者保存状态下取值函数
        /// </summary>
        /// <param name="info"></param>
        private void SetInfo(User_Group info)
        {
            info.group_id =Convert.ToInt32( this.txtGroupID.Text);
            info.group_name = this.txtGroupName.Text;
            info.parent_id = Convert.ToInt32(this.lookupParentID.EditValue);
            info.remark = this.memoRemark.Text;
        }

        /// <summary>
        /// 数据字典加载
        /// </summary>
        private void InitDictItem()
        {
            this.lookupParentID.Properties.DataSource = controller.GetAllUserGroups(); 
        }

        public override bool CheckInput()
        {
            if (string.IsNullOrEmpty(this.txtGroupID.Text.Trim()))
            {
                ExMessageBox.Show("提示信息：角色ID不能为空！", AppSetter.SoftName);
                return false;
            }

            if (string.IsNullOrEmpty(this.txtGroupName.Text.Trim()))
            {
                ExMessageBox.Show("提示信息：角色名称不能为空！", AppSetter.SoftName);
                return false;
            }

            return true;
        }

        public override void DisplayData()
        {
            InitDictItem();

            if (!string.IsNullOrEmpty(EntityID))
            {
                User_Group info = controller.GetGroupByID(this.CurrentGroupID);
                #region 显示用户信息
                
                this.txtGroupID.Focus();
                if (null != info)
                {
                    this.txtGroupID.Text = info.group_id.ToString();
                    this.txtGroupID.Enabled = false;
                    this.txtGroupName.Text = info.group_name;
                    this.lookupParentID.EditValue = info.parent_id;
                    this.memoRemark.Text = info.remark;
                }

                #endregion
            }
            else
            {
                this.txtGroupID.Focus();
            };
        }

        public override bool SaveAddNew()
        {
            User_Group group = new User_Group();
            this.SetInfo(group);
            controller.AddUserGroup(group);
            LogHelper.Log("角色管理", "新增", "", "group:" +
            group.group_id.ToString());
            (this.Owner.ActiveMdiChild as FrmUserGroup).RefreshUserGroup();
            return true;
        }

        public override bool SaveUpdated()
        {
            User_Group group = new User_Group();
            this.SetInfo(group);
            controller.EditUserGroup(group);
            LogHelper.Log("角色管理", "编辑", "", "group:" +
                group.group_id.ToString());
            (this.Owner.ActiveMdiChild as FrmUserGroup).RefreshUserGroup();
            return true;
        }
    }
}
